﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DbMapping.Common;
using VideoCache.Server.BLL;
using VideoCache.Server.Model;
using VideoCache.Server.MVCWeb.Areas.Member.Models;
using System.Web.Security;
using VideoCache.Server.MVCWeb.Attributes;

namespace VideoCache.Server.MVCWeb.Areas.Member.Controllers
{
    public class UserController : Controller
    {
        //
        // GET: /Member/User/

        public ActionResult Index()
        {
            return View();
        }

        [ActionAuthorize(AuthorizeType.Member)]
        public ActionResult Edit()
        {
            return View();
        }

        [ActionAuthorize(AuthorizeType.Member)]
        [HttpPost]
        public ActionResult Edit(UserModel model)
        {
            if (ModelState.IsValid)
            {

            }

            return View(model);
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (UserBLL.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, model.UserName, DateTime.Now, DateTime.Now.AddDays(1), false, Request.UserHostAddress);

                    string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie authCookie = new System.Web.HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                    this.HttpContext.Response.Cookies.Add(authCookie);

                    if (string.IsNullOrEmpty(returnUrl))
                    {
                        return RedirectToAction("Index", "Home", new { area = "Member" });
                    }
                    else
                    {
                        return Redirect(returnUrl);
                    }
                }
            }

            ModelState.AddModelError("Error", "账号或密码错误。");

            return View(model);
        }

        public ActionResult ForgotPassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ForgotPassword(string email)
        {
            return View();
        }

        public ActionResult ChangePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            if (ModelState.IsValid)
            {
                string userName = HttpContext.User.Identity.Name;

                if (UserBLL.ValidateUser(userName, model.OldPassword))
                {
                    UserInfo info = UserBLL.GetUserByName(userName);

                    info.Password = model.NewPassword;

                    UserBLL.Update(info);

                    return RedirectToAction("ChangePasswordSuccess");
                }
                else
                {
                    ModelState.AddModelError("Error", "旧密码错误。");
                }
            }

            return View(model);
        }

        /// <summary>
        /// 登出
        /// </summary>
        /// <returns></returns>
        public ActionResult Logout()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction("Index", "Home", new { area = "" });
        }

        [HttpPost]
        public ActionResult IsUserNameAvaliable(string userName)
        {
            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, UserInfo.UserNameField, CompareType.Equal, userName));

            return Json(UserBLL.Exists(cons) == false);
        }

        [HttpPost]
        public ActionResult IsEmailAvaliable(string email)
        {
            Conditions cons = new Conditions();
            cons.Add(new ConditionItem(LinkType.And, UserInfo.EmailField, CompareType.Equal, email));

            return Json(UserBLL.Exists(cons) == false);
        }
    }
}